package edu.mit.simile;


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import de.fuberlin.wiwiss.ng4j.NamedGraph;
import de.fuberlin.wiwiss.ng4j.NamedGraphSet;
import de.fuberlin.wiwiss.ng4j.impl.NamedGraphSetImpl;
import de.fuberlin.wiwiss.ng4j.trig.TriGWriter;

public class NamedGraphCreate {
    static ModelLoader loader = null ;
    static Model _imodel = null ;
    static Model _cmodel = null ;
    private static Properties prop = null ;
    static String outputModeleName="";

    public static void main(String[] args) throws IOException {
            prop = new Properties();
        try {
        //load a properties file
             prop.load(new FileInputStream("config/rdf2okkam.properties"));
             outputModeleName=prop.getProperty("outputModeleName");

     } catch (IOException ex) {
             ex.printStackTrace();
             System.out.println("File not found");
 }

            // Create a new graphset
            NamedGraphSet graphset = new NamedGraphSetImpl();

            // Create a new NamedGraph in the NamedGraphSet
            
            loader = ModelLoader.getInstance() ; 
            _imodel = ModelLoader.getInstance().getInputModel();
            _cmodel = ModelLoader.getInstance().getContextModel();
            String datiGraphURI="http://www.okkam.org/folgaria/anagrafe/20120607/10";
            
            NamedGraph dgraph = graphset.createGraph(datiGraphURI);
    
            StmtIterator stmtit=_imodel.listStatements();
            
            buildGraph(dgraph, stmtit);        
            
           // String contextGraphURI="http://www.okkam.org/context/folgaria/anagrafe/20110607/10";
         //   NamedGraph cgraph = graphset.createGraph(contextGraphURI);
         //   stmtit=_cmodel.listStatements();
            
          //  buildGraph(cgraph, stmtit);
            // Create a quad
            

            // Add the quad to the graphset. This will create a new NamedGraph in the
            // graphset.
            //graphset.addQuad(quad);
            //
            OutputStream out = new FileOutputStream(outputModeleName);
            
            String riv = "http://www.semanticweb.org/ontologies/2011/9/Rivela-Flussi.owl#" ;
            String rivPrefix = "riv" ;
            
            String aco="http://example.org/access.owl#";
            String acoPrefix="aco";
            
            TriGWriter trigwriter=new TriGWriter();
            
            trigwriter.addNamespace(rivPrefix, riv);
            trigwriter.addNamespace(acoPrefix, aco);
            
            trigwriter.write(graphset, out, null);
            //trigwriter.write(graphset, System.out, null);
            
    }
    public static void buildGraph(NamedGraph graph, StmtIterator it){ 
            while(it.hasNext()){
                    Statement s=it.next();
                    String subject=s.getSubject().toString();
                    String predicate=s.getPredicate().toString();
                    String object=object=s.getObject().toString();
                    if(s.getObject().isLiteral())
                            {
                            graph.add(new Triple(Node.createURI(subject),
                                            Node.createURI(predicate),        
                                            Node.createLiteral(object)));
                            }
                    else 
                    graph.add(new Triple(Node.createURI(subject),
                                    Node.createURI(predicate),        
                                    Node.createURI(object)));
            }
    }
}